package com.fulihui.information.biz.counter;

import com.fulihui.information.core.db.CategoryStatisticsDB;
import com.fulihui.information.core.db.ContentCounterDB;
import com.fulihui.information.core.db.ContentListItemDB;
import com.fulihui.information.dal.dataobj.InfoContent;
import com.fulihui.information.dal.mapper.ExtInfoContentListItemMapper;
import com.fulihui.information.dto.ContentListItemDTO;
import com.fulihui.information.model.UserContentOperate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Collections;
import java.util.List;

import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author Willard.Hu on 2016/12/30 0030.
 */
public abstract class AbstractContentOperableCounter implements OperableCounter<UserContentOperate, Long, String, Long> {
    protected final transient Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    @Qualifier("redisContentCounterDB")
    protected ContentCounterDB             contentCounterDB;
    @Autowired
    protected ContentListItemDB            contentListItemDB;
    @Autowired
    protected ExtInfoContentListItemMapper extInfoContentListItemMapper;
    @Autowired
    protected CategoryStatisticsDB         categoryStatisticsDB;

    protected void incrCategoryStatistics(String contentId, CategoryStatisticsDB.StatisticType type) {
        List<ContentListItemDTO> content = contentListItemDB.queryList(Collections.singletonList(contentId));
        String categoryCode;
        if (!isEmpty(content)) {
            categoryCode = content.get(0).getCategoryCode();
        } else {
            InfoContent infoContent = extInfoContentListItemMapper.selectListItemById(contentId);
            if(infoContent == null){
                return;
            }
            categoryCode = infoContent.getCategoryCode();
        }
        categoryStatisticsDB.incr(categoryCode, type);
    }

}
